const express = require('express');
const https = require('https');
const fs = require('fs');
const path = require('path');
const cors = require('cors');
const apiRoutes = require('./routes/api');

const app = express();

// 启用CORS
app.use(cors());

// 静态文件服务
app.use(express.static(path.join(__dirname, 'public')));

// 解析JSON请求体
app.use(express.json());

// API路由
app.use('/api', apiRoutes);

// HTTPS配置
const httpsOptions = {
    key: fs.readFileSync(path.join(__dirname, 'localhost-key.pem')),
    cert: fs.readFileSync(path.join(__dirname, 'localhost.pem'))
};

// 创建HTTPS服务器
const server = https.createServer(httpsOptions, app);

// 设置端口
const PORT = process.env.PORT || 3000;

// 启动服务器
server.listen(PORT, () => {
    console.log(`服务器运行在 https://localhost:${PORT}`);
    console.log('注意：由于使用自签名证书，浏览器可能会显示安全警告，请在开发环境中手动接受证书');
}); 